<?php

namespace App\Http\Controllers\Finance;


use App\Models\Finance;
use App\Models\System\SysLog;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Finance\FinanceShowRequest;
use App\Http\Requests\Finance\FinanceIndexRequest;
use App\Http\Requests\Finance\FinanceStoreRequest;
use App\Http\Requests\Finance\FinanceUpdateRequest;
use App\Http\Requests\Finance\FinanceReportRequest;
use App\Http\Requests\Finance\FinanceDestroyRequest;

/**
 * 财务管理逻辑控制器
 *
 * Date: 2018/6/8
 * @author George
 * @package App\Http\Controllers\Finance
 */
class FinanceController extends Controller
{
    /**
     * 财务记录列表
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceIndexRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(FinanceIndexRequest $request)
    {
        $paginate = $request->get('paginate',10);
        $where = array_filter($this->extractInputFromRules($request,$request->rules()));
        $where['company_id'] = $request->user()->company->id;
        $list = Finance::query()->where($where)->paginate($paginate);
        return success($list);
    }

    /**
     * 新增财务记录
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceStoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(FinanceStoreRequest $request)
    {
        $attr = $this->extractInputFromRules($request,$request->rules());
        $attr['date'] = explode('-',$attr['date']);
        $attr['year'] = $attr['date'][0];
        $attr['month'] = $attr['date'][1];
        unset($attr['date']);
        $attr['company_id'] = $request->user()->company->id;
        $finance = Finance::query()->create($attr);
        SysLog::log(Auth::user(),'财务记录模块','创建财务记录');
        return stored($finance);
    }

    /**
     * 查询单条财务记录
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceShowRequest $request
     * @param Finance $finance
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(FinanceShowRequest $request,Finance $finance)
    {
        $finance->date = $finance->year.'-'.$finance->month;
        return success($finance);
    }


    /**
     * 财务记录修改
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceUpdateRequest $request
     * @param Finance $finance
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(FinanceUpdateRequest $request,Finance $finance)
    {
        $attr = $this->extractInputFromRules($request,$request->rules());
        $attr['date'] = explode('-',$attr['date']);
        $attr['year'] = $attr['date'][0];
        $attr['month'] = $attr['date'][1];
        unset($attr['date']);
        foreach ($attr as $k => $v){
            $finance->$k = $v;
        }
        $finance->saveOrFail();
        $finance->date = $finance->year.'-'.$finance->month;
        return updated($finance);
    }

    /**
     * 删除财务记录
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceDestroyRequest $request
     * @param Finance $finance
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(FinanceDestroyRequest $request,Finance $finance)
    {
        $finance->delete();
        return deleted();
    }

    /**
     * 财务总览报表
     *
     * Date: 2018/5/21
     * @author Sam
     * @param FinanceReportRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function financeReport(FinanceReportRequest $request)
    {
        $where = $this->extractInputFromRules($request,$request->rules());
        $where['company_id'] = $request->user()->company->id;
        $monthly = Finance::query()->selectRaw('sum(amount) as sum,type,month')->where($where)->groupBy('type','month')->get()->sortBy('month');
        $list = [];
        if($monthly->isNotEmpty()){
            for ($i = 1; $i < 13; $i++) {
                $list[] = ['month'=>$i,'income'=>0,'pay'=>0];
            }
            foreach ($monthly as $k => $v){
                foreach ($list as $k1 =>$v1){
                    if($v1['month']==$v['month']){
                        $list[$k1][$v['type']] = $v['sum'];
                    }
                }
            }
        }
        return success($list);
    }
}
